package top.moonkit.rs_pro.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.LocaleResolver;

import java.util.Locale;

@RestController
@RequestMapping("/language")
public class LanguageController {
    
    private final LocaleResolver localeResolver;
    
    public LanguageController(LocaleResolver localeResolver) {
        this.localeResolver = localeResolver;
    }
    
    @GetMapping("/switch")
    public String switchLanguage(HttpServletRequest request, HttpServletResponse response,
                                 @RequestParam String lang) {
        Locale locale;
        switch (lang) {
            case "en":
                locale = Locale.ENGLISH;
                break;
            case "zh":
            default:
                locale = Locale.SIMPLIFIED_CHINESE;
                break;
        }
        localeResolver.setLocale(request, response, locale);
        return "Language switched to: " + lang;
    }
}